import numpy as np
import random
def initProper(nx,ny,init_id=0):
    # xmin,xmax,ymin,ymax,id,current,
    ret_proper=np.ndarray(shape=(nx*ny,8), dtype=int)
    ret_proper[:,5]=1
    ret_proper[:,4]=np.array(list(range(init_id,init_id+nx*ny)))
    return ret_proper

def splitArray(image_array,image_width,image_height,ret_proper,nx=2,ny=2):
    ret_image_arrays=[]
    xpieces=np.array_split(image_array,nx,axis=1)
    for xpiece in xpieces:
        ypieces=np.array_split(xpiece,ny,axis=0)
        ret_image_arrays.extend(ypieces)
    
    stdposes=np.ndarray((ny*nx,2),dtype=int)
    one_y=int(image_height/ny)
    one_x=int(image_width/nx)
    acc_y=0;acc_x=0
    for i,piece in enumerate(ret_image_arrays):
        stdposes[i,0]=acc_x
        stdposes[i,1]=acc_y
        if acc_y+piece.shape[0]<image_height:
            acc_y+=piece.shape[0]
        else:
            acc_y=0
            acc_x+=piece.shape[1]
    ret_proper[:,6:8]=stdposes

    for i,piece in enumerate(ret_image_arrays):
        rand = i
        while rand == i:
            rand=random.randint(0,len(ret_image_arrays)-1)
        ret_proper[i,0]=ret_proper[rand,6]
        ret_proper[i,1]=ret_proper[i,0]+piece.shape[1]
        ret_proper[i,2]=ret_proper[rand,7]
        ret_proper[i,3]=ret_proper[i,2]+piece.shape[0]

    return ret_image_arrays, ret_proper, one_x, one_y
